from datetime import datetime

from django.db import models

from organization.models import CourseOrg, Teacher

# Create your models here.


class Course(models.Model):
    course_org = models.ForeignKey(CourseOrg, verbose_name='课程机构', null=True, blank=True)
    name = models.CharField(max_length=50, verbose_name='课程名')
    desc = models.CharField(max_length=300, verbose_name='课程描述')
    detail = models.TextField(verbose_name='课程详情')
    degree = models.CharField(choices=(('cj', '初级'), ('zj', '中级'), ('gj', '高级')), verbose_name='难度', max_length=5)
    learn_times = models.IntegerField(default=0, verbose_name='学习时间')
    students = models.IntegerField(default=0, verbose_name='学习人数')
    fav_nums = models.IntegerField(default=0, verbose_name='收藏人数')
    image = models.ImageField(max_length=100, verbose_name='封面图', upload_to='course/%Y/%m')
    click_nums = models.IntegerField(default=0, verbose_name='点击数')
    category = models.CharField(max_length=20, verbose_name='课程类别', default='后端开发')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')
    tag = models.CharField(default='', verbose_name='课程标签', max_length=20)
    teacher = models.ForeignKey(Teacher, verbose_name='授课讲师', null=True, blank=True)
    need_know = models.CharField(max_length=300, verbose_name='课程须知', null=True, blank=True)
    what_can_learn = models.CharField(max_length=300, verbose_name='老师告诉你能学到什么', null=True, blank=True)
    is_banner = models.BooleanField(default=False, verbose_name='是否轮播')

    class Meta:
        verbose_name = '课程'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    def get_zj_num(self):
        # 获取章节数量
        return self.lesson_set.all().count()

    def get_course_user(self):
        # 获取当前课程下的用户
        return self.usercourse_set.all()[:5]

    def get_course_lesson_active(self):
        # 获取课程下的前两章节信息，用来在课程信息上面处于选中状态并且展示下面的video信息
        return self.lesson_set.all()[:2]

    def get_course_lesson_unactive(self):
        # 获取课程下的前两章节以后的章节信息，用来在课程信息上面处于未选中的状态，只展示章节名称
        return self.lesson_set.all()[2:]


class Lesson(models.Model):
    course = models.ForeignKey(Course, verbose_name='课程')
    name = models.CharField(max_length=100, verbose_name='章节名')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

    class Meta:
        verbose_name = '章节'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    def get_course_video(self):
        return self.video_set.all()

class Video(models.Model):
    lesson = models.ForeignKey(Lesson, verbose_name='章节')
    name = models.CharField(max_length=100, verbose_name='视频名')
    url = models.URLField(verbose_name='访问地址', default='')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')


    class Meta:
        verbose_name = '视频'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class CourseResourse(models.Model):
    course = models.ForeignKey(Course, verbose_name='课程')
    name = models.CharField(max_length=100, verbose_name='名称')
    download = models.FileField(max_length=100, verbose_name='课程资源下载地址', upload_to='course/resource/%Y/%m')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

    class Meta:
        verbose_name = '课程资源'
        verbose_name_plural = verbose_name